<!--确认境外方案 列表 填报方案-->
<template>
  <div
    class="confirm-outbound-box"
    v-loading="loading"
    element-loading-customClass="ne-loading"
    element-loading-background="rgba(255, 255, 255, 0.6)"
  >
    <el-row style="margin-bottom: 0.12rem">
      <el-col
        :span="24"
        class="text-right clearfix ne-title"
        style="padding: 0 0 0.05rem 0.1rem"
      >
        <p class="left">
          {{ title }}
        </p>
        <el-button
          v-show="isSureScheme"
          type="primary"
          size="medium"
          @click="sendFA"
          >确定</el-button
        >
        <el-button
          v-show="!isSureScheme"
          type="primary"
          size="medium"
          @click="saveFA"
          >提交</el-button
        >
        <el-button
          v-show="!isSureScheme"
          type="primary"
          size="medium"
          @click="addFA"
          >添加方案</el-button
        >
        <el-button v-show="!isSureScheme" type="primary" size="medium"
          >删除方案</el-button
        >
        <el-button v-show="!isSureScheme" type="primary" size="medium"
          >复制方案</el-button
        >
        <el-button type="primary" size="medium" style="display: none"
          >上传附件</el-button
        >
        <el-button size="medium" @click="closeFun">关闭</el-button>
      </el-col>
    </el-row>
    <DicTable
      stripe
      highlight-current-row
      highlight-hover-row
      max-height="600"
      ref="COtable"
      border
      header-row-class-name="header-row-class"
      row-class-name="ne-row-class"
      :columnData="COtbs.tableColumns"
      :tableData="COtbs.tableData"
      :loading="COtbs.tableLoading"
      :currentPage="COtbs.tablePage.page"
      :pageSize="COtbs.tablePage.pageSize"
      :totalResult="COtbs.tablePage.totalResult"
      :pager="false"
      @on-page-change="handlePageChange"
    >
      <template #projectType="{ row }">
        <a @click="openDialog(row)" class="blue ne-scheme-type">{{
          "方案" + row.projectType
        }}</a>
      </template>
    </DicTable>
    <el-dialog
      :title="dialogTitle"
      :close-on-click-modal="false"
      append-to-body
      v-if="dialogVisible"
      :visible.sync="dialogVisible"
      width="95%"
    >
      <modify-scheme
        :isSureScheme="isSureScheme"
        :row="selRowData"
        :locationData="locationData"
        :projectArr="projectObj['p' + projectType]"
        :projectType="projectType"
        :ifAdd="ifAdd"
        :allInfo="{ ...CTOInfo, ...ODKInfo }"
        :show.sync="dialogVisible"
      ></modify-scheme>
    </el-dialog>
  </div>
</template>
<script>
import DicTable from "@/components/public/DicTable";
import ModifyScheme from "@/components/custorder/pengding/ModifyScheme";
import requestUtil from "@/utils/requestEnc";
import CustOrderService from "@/api/order/CustOrderService";
import { mapState } from "vuex";
export default {
  name: "ConfirmOutbound",
  data() {
    return {
      loading: false,
      isSureScheme: null,
      title: "",
      dialogTitle: "",
      dialogVisible: false,
      projectType: 0,
      ifAdd: false,
      selRowData: [],
      projectObj: {},
      nodeId: "",
      locationId: "",
      locationData: {},
      COtbs: {
        tableColumns: [
          { type: "radio", width: 80, align: "center" },
          { slotName: "projectType", title: "方案类型" },
          { field: "routeTypeName", title: "通达方式" },
          { field: "exportOgrName", title: "出入境ITMC" },
          { field: "exportPopName", title: "海外登陆POP点" },
          { field: "overseasNbr", title: "海外公司自编号" },
          { field: "maintainLevelFlag", title: "电路维护等级是否具备" },
          { field: "maintainComment", title: "维护等级描述" },
          { field: "actDate", title: "有效期" },
          { field: "isWrongOrder", title: "错单标识" },
        ],
        tableData: [
          // { id: 10001, schemeType: '方案1', regionName: '直达方式',
          //   custName: '广州', custControlLevel: '香港', custAddr: 'hfg',
          //   custManagerNameA:'是',contactName:'sdfs',contactMobilePhone:'2021-05-18 00:00:00',operation:'否'},
          // { id: 10002, schemeType: '方案2', regionName: '直达方式',
          //   custName: '广州', custControlLevel: '香港', custAddr: 'hfg',
          //   custManagerNameA:'是',contactName:'sdfs',contactMobilePhone:'2021-05-18 00:00:00',operation:'否'}
        ],
        tableLoading: false,
        tablePage: {
          currentPage: 1,
          pageSize: 10,
          totalResult: 0,
        },
      },
    };
  },
  props: {
    ODKInfo: {
      type: [Object],
    },
    CTOInfo: {
      type: [Object, Array],
    },
  },
  mounted() {},
  computed: {
    ...mapState("user", ["user"]),
  },
  created() {
    this.getDetail();
    if (this.ODKInfo.tacheId == "TACHE1005") {
      this.isSureScheme = true;
    } else {
      this.isSureScheme = false;
    }
  },
  components: { DicTable, ModifyScheme },
  methods: {
    addFA() {
      this.ODKInfo.type = "0";
      this.dialogTitle = "添加方案";
      this.ifAdd = true;
      this.dialogVisible = true;
    },
    async getDetail() {
      this.loading = true;
      let base = {
        userName: this.user.staffName,
        userId: this.user.staffId,
        operOrgId: this.user.orgId,
        oneOrmany: "one",
        productType: "3",
        nodeType: "50160002",
      };
      let { batchNbr, custSoNumber, orderItemId, custId } = this.CTOInfo,
        { taskId, activityId, processInstId, orderAuditId } = this.ODKInfo;
      // 判断境外方案是否是A端
      if (taskId == "TACHE10050002" || taskId == "TACHE10050001") {
        this.COtbs.tableColumns = [
          { type: "radio", width: 80, align: "center" },
          { field: "orgName", title: "填报单位" },
          { slotName: "projectType", title: "方案类型" },
          { field: "routeName", title: "通达方式" },
          { field: "exportOgrName", title: "出入境ITMC" },
          { field: "exportPopName", title: "海外登陆POP点" },
          { field: "overseasNbr", title: "海外公司自编号" },
          { field: "maintainLevelFlag", title: "电路维护等级是否具备" },
          { field: "maintainComment", title: "维护等级描述" },
          { field: "actDate", title: "有效期" },
          { field: "isWrongOrder", title: "错单标识" },
        ];
      }
      this.projectObj = [];
      if (
        taskId == "TACHE10040001" ||
        taskId == "TACHE10040012" ||
        taskId == "TACHE10040010" ||
        taskId == "TACHE10050001" ||
        taskId == "TACHE10040014"
      ) {
        base.nodeType = "50160001";
      }
      //确认境外方案的时候查询不传操作组织 查询所有组织填报的方案
      if (taskId == "TACHE10050002" || taskId == "TACHE10050001") {
        delete base.operOrgId;
      }
      const result = await CustOrderService.qryOutboundDetail({
        batchNbr,
        custSoNumber,
        orderItemId,
        custId,
        taskId,
        activityId,
        processInstId,
        orderAuditId,
        ...base,
      });
      
      // this.COtbs.tableData = result.meta.solutionInterProposes;
      this.COtbs.tableData = this.tableDataHandle(
        result.meta.solutionInterProposes
      );
      this.projectType = this.COtbs.tableData.length;
      if (result.meta.solutionCircuitNodes) {
        this.nodeId = result.meta.solutionCircuitNodes[0].nodeId;
      }
      if (result.meta.politicalLocations) {
        this.locationId = result.meta.politicalLocations[0].locationId;
        this.locationData = result.meta.politicalLocations[0];
      }
      this.loading = false;
      // this.title = result.meta.solutionCircuitNodes[0].nodeName;
      // this.$emit('update:title',result.meta.solutionCircuitNodes[0].nodeName);
    },
    openDialog(row) {
      this.dialogTitle = "修改方案";
      this.ifAdd = false;
      this.projectType = row.projectType;
      this.dialogVisible = true;
    },
    handlePageChange({ currentPage, pageSize }) {
      this.COtbs.tablePage.currentPage = currentPage;
      this.COtbs.tablePage.pageSize = pageSize;
    },
    tableDataHandle(data) {
      let newarr = [];
      data.forEach((item, index) => {
        if (!item.projectType) return;
        let projectType = item.projectType;
        if (!this.projectObj["p" + projectType]) {
          this.projectObj["p" + projectType] = [];
        }
        this.projectObj["p" + projectType].push(item);
      });
      for (var key in this.projectObj) {
        let item2 = this.projectObj[key];
        newarr.push(item2[0]);
      }
      return newarr;
    },
    //提交方案 只需要将任务处理下去即可
    async saveFA() {
      if (this.COtbs.tableData.length <= 0) {
        this.$confirm("请先添加方案！", "提示", {
          confirmButtonText: "确定",
          showCancelButton: false,
          type: "warning",
          center: true,
        });
        return;
      }
      this.loading = true;
      this.projectObj = [];
      let base = {
        userName: this.user.staffName,
        userId: this.user.staffId,
        orgId: this.user.orgId,
        oneOrmany: "one",
        productType: "3",
      };
      let { batchNbr, custSoNumber, orderItemId, custId } = this.CTOInfo,
        orderItemIds = orderItemId,
        { taskId, activityId, processInstId, orderAuditId } = this.ODKInfo;
      let orderTasks = this.buildOrderTasks(
        custId,
        taskId,
        this.user.orgId,
        this.user.staffId,
        orderItemId
      );
      let allParam = {
        orderAuditId,
        orderItemId,
        custId,
        custSoNumber,
        activityId,
        processInstId,
        taskId,
        ...base,
        ...{ orderTasks: orderTasks },
      };
      const result = await CustOrderService.fillAbroadPropose(allParam);
      this.loading = false;
      try {
        if (result.meta.resultCode == 0) {
          this.$message({
            type: "success",
            message: "保存成功!",
          });
          this.$emit("update:show", false);
        } else {
          this.$message({
            showClose: true,
            message: result.meta.resultMsg,
            type: "error",
          });
        }
      } catch {
        this.$message({
          showClose: true,
          message: "code:" + result.code + "  msg:" + result.msg,
          type: "error",
        });
      }
    },
    // 确定方案
    async sendFA() {
      let selRow = this.$refs["COtable"].$refs["vxeTable"].getRadioRecord();
      if (!selRow) {
        this.$confirm("请选择要确认的方案！", "提示", {
          confirmButtonText: "确定",
          showCancelButton: false,
          type: "warning",
          center: true,
        });
        return false;
      }
      this.loading = true;
      let base = {
        productType: "3",
        oneOrmany: "one",
        userId: this.user.staffId,
      };
      let {
          prodOfferId,
          batchNbr,
          custSoNumber,
          custOrderId,
          orderItemId,
          custId,
        } = this.CTOInfo,
        { taskId, activityId, processInstId, orderAuditId } = this.ODKInfo;
      let orderTasks = this.buildOrderTasks(
        custId,
        taskId,
        this.user.orgId,
        this.user.staffId,
        orderItemId
      );
      let solutionInterProposes = [
        {
          actType: "MOD",
          confirmFlag: "1",
          actFlag: "1",
          confirmUserId: this.user.staffId,
          confirmUserName: this.user.staffName,
          confirmDate: this.$util.dateFormat(new Date(), "yyyy-MM-dd"),
          lastModifyBy: this.user.staffId,
          lastModifyDate: this.$util.dateFormat(new Date(), "yyyy-MM-dd"),
          orderItemId: orderItemId,
          nodeId: this.nodeId,
          projectType: selRow.projectType,
        },
      ];
      let confirmOther = {
        custOrderId: custOrderId,
        orderItemId: orderItemId,
        custSoNumber: custSoNumber,
        prodOfferId: prodOfferId,
        batchNbr: batchNbr,
        itmeId: this.locationId,
        taskId: taskId,
        nodeId: this.nodeId,
        userId: this.user.staffId,
        staffId:this.user.staffId,
        custId: custId,
      };
      let allParam = {
        custOrderId,
        orderItemId,
        taskId,
        custSoNumber,
        custId,
        activityId,
        processInstId,
        orderAuditId,
        ...base,
        ...{
          orderTasks: orderTasks,
          solutionInterProposes: solutionInterProposes,
          confirmOther: confirmOther,
        },
      };
      const result = await CustOrderService.confirmPropose(allParam);
      this.loading = false;
      try {
        if (result.meta.resultCode == 0) {
          this.$message({
            type: "success",
            message: "确认成功!",
          });
          this.$emit("update:show", false);
        } else {
          this.$message({
            showClose: true,
            message: result.meta.resultMsg,
            type: "error",
          });
        }
      } catch {
        this.$message({
          showClose: true,
          message: "code:" + result.code + "  msg:" + result.msg,
          type: "error",
        });
      }
    },
    buildOrderTasks(custId, taskId, orgId, userId, orderItemId) {
      let orderTask = {};
      let orderTasks = [];
      orderTask["taskActDate"] = this.$util.dateFormat(
        new Date(),
        "yyyy-MM-dd"
      );
      orderTask["taskOprDate"] = this.$util.dateFormat(
        new Date(),
        "yyyy-MM-dd"
      );
      orderTask["actType"] = "MOD";
      orderTask["taskId"] = taskId + "";
      orderTask["oprOrgId"] = orgId + "";
      orderTask["staffId"] = userId + "";
      orderTask["custId"] = custId + "";
      orderTask["result"] = "9005001";
      orderTask["orderItemId"] = orderItemId + "";
      orderTask["taskComment"] = "填报方案/确认方案";
      orderTasks.push(orderTask);
      return orderTasks;
    },
    closeFun() {
      this.$emit("update:show", false);
    },
  },
};
</script>

<style lang="scss" scoped>
.confirm-outbound-box {
  height: 84vh;
  overflow-y: auto;
  .ne-scheme-type {
    &:hover {
      text-decoration: underline;
      color: $blue;
    }
  }
}
</style>
